using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace VoxelEngine
{
	public class LongPressFeedBackController : MonoBehaviour
	{
		[SerializeField]
		private Image _outerCircleImage;

		[SerializeField]
		private Image _innerCircleImage;

		[SerializeField]
		private Camera _uiCamera;

		[SerializeField]
		private RectTransform _canvasTransform;

		private Vector3 _scale = Vector3.zero;

		private Vector3 _position = Vector3.zero;

		private IEnumerator _currentFade;

		private Color _auxColor = Color.white;

		private void SetScreenPosition(Vector2 position)
		{
			if (_canvasTransform == null)
			{
				Vector3 vector = _uiCamera.ScreenToWorldPoint(position);
				_position = base.transform.position;
				_position.x = vector.x;
				_position.y = vector.y;
				base.transform.position = _position;
			}
			else
			{
				RectTransformUtility.ScreenPointToLocalPointInRectangle(_canvasTransform, position, null, out Vector2 localPoint);
				base.transform.GetComponent<RectTransform>().localPosition = localPoint;
			}
		}

		public void Show(Vector2 position)
		{
			if (_currentFade != null)
			{
				StopCoroutine(_currentFade);
			}
			base.gameObject.SetActive(value: true);
			SetScreenPosition(position);
			_innerCircleImage.transform.localScale = new Vector3(0f, 0f, 0f);
			_currentFade = Fade(0f, 0.5f, 0.1f, null);
			StartCoroutine(_currentFade);
		}

		public void Move(Vector2 position)
		{
			SetScreenPosition(position);
		}

		public IEnumerator Fade(float initial, float final, float time, Action complete)
		{
			float currTime = initial * time;
			Color white = Color.white;
			SetAlpha(initial);
			while (currTime < time)
			{
				float progress = currTime / time;
				float alpha = final * progress;
				SetAlpha(alpha);
				currTime += Time.deltaTime;
				yield return null;
			}
			SetAlpha(final);
			complete.Fire();
		}

		private void SetAlpha(float alpha)
		{
			_auxColor.a = alpha;
			_outerCircleImage.color = _auxColor;
			_innerCircleImage.color = _auxColor;
		}

		public void Hide()
		{
			if (base.gameObject.activeSelf)
			{
				StopCoroutine(_currentFade);
				Color color = _outerCircleImage.color;
				_currentFade = Fade(color.a, 0f, 0.1f, delegate
				{
					base.gameObject.SetActive(value: false);
				});
				StartCoroutine(_currentFade);
			}
		}

		public void UpdateProgress(float progress)
		{
			_scale.x = progress;
			_scale.y = progress;
			_scale.z = progress;
			_innerCircleImage.transform.localScale = _scale;
		}
	}
}
